import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { BooksService } from './books.service';
import { CreateBookDto } from './dto/create-book.dto';
import { UpdateBookDto } from './dto/update-book.dto';
import { Result } from 'src/utils/Result';
import { ApiBody, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';

@ApiTags('图书模块')
@Controller('books')
export class BooksController {
  constructor(private readonly booksService: BooksService) {}
  @ApiBody({ type: CreateBookDto })
  @Post('/add')
  async create(@Body() createBookDto: CreateBookDto) {
    const result = await this.booksService.create(createBookDto);
    return Result.success(result)
  }
  @ApiBody({ schema: {
    default: {pageIndex: 1,pageSize: 30,book_name: ''}
  } })
  @Post('list')
  async findAll(@Body() data: {pageIndex:number,pageSize: number,book_name: string}) {
    const result = await this.booksService.findAll(data);
    return Result.success(result)
  }
  @ApiQuery({ name: 'id', required: true, description: '图书编号'})
  @Get('detail/:id')
  async findOne(@Param('id') id: string) {
    const result = await this.booksService.findOne(+id);
    return Result.success(result)
  }
  
  @ApiParam({ name: 'id', required: true, description: '图书编号'})
  @ApiBody({ type: UpdateBookDto })
  @Patch('update/:id')
  async update(@Param('id') id: string, @Body() updateBookDto: UpdateBookDto) {
    const result = await this.booksService.update(+id, updateBookDto);
    return Result.success(result)
  }
  @ApiParam({ name: 'id', required: true, description: '图书编号'})
  @Delete('del/:id')
  async remove(@Param('id') id: string) {
    const result = await this.booksService.remove(+id);
    return Result.success(result)
  }
}
